package com.mujin.keji.collectionsystem.module.main.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.listeners.FunctionItemClickListener;
import com.mujin.keji.collectionsystem.module.main.dto.FunctionDto;

import java.util.List;


/**
 * 首页下半部分功能适配器
 *
 * @author weishixiong
 * @Time 2018-03-20
 */
public class BottomFunctionAdapter extends RecyclerView.Adapter<BottomFunctionAdapter.ViewHolder> {

    private Activity mContext;
    private List<FunctionDto> datas;
    private FunctionItemClickListener listener;

    public BottomFunctionAdapter(Activity context, List<FunctionDto> datas, FunctionItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bottomfunction, null, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tv_function.setText(datas.get(position).getName());
        holder.iv_function.setImageResource(datas.get(position).getResource());
        holder.tv_function.setTextColor(Color.parseColor(datas.get(position).getColor()));
        holder.ll_function.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(datas.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_function;
        public TextView tv_function;
        public LinearLayout ll_function;


        public ViewHolder(View itemView) {
            super(itemView);
            iv_function = (ImageView) itemView.findViewById(R.id.iv_function);
            tv_function = (TextView) itemView.findViewById(R.id.tv_function);
            ll_function = (LinearLayout) itemView.findViewById(R.id.ll_function);


        }

    }
}
